<?php
namespace app\teacher\controller;

use app\teacher\controller\PublicController;
use app\common\model\TemplateModel;
use app\common\model\TemplateAttrModel;

//模板
class TemplateController extends PublicController
{

    protected $targets = ["_blank" => "新标签页打开", "_self" => "本窗口打开"];

	public function __construct(){
		parent::__construct();
		$this->check();
		$this->ids = $this->install();
	}

	public function index(){
		$param = $this->request->param();
		$templates = TemplateModel::where('user_id','in',$this->ids)->paginate(20,false,['query'=>$param]);
		$this->assign('templates',$templates);
		return $this->fetch('teacher@template/index');
	}

    public function add()
    {
        $this->assign('targets', $this->targets);
        return $this->fetch();
    }


    public function addPost()
    {
        $data      = $this->request->param();
		$data['addtime']=time();
        $data['user_id'] = $this->user_id;
        $TemplateModel = new TemplateModel();
        $result    = $TemplateModel->validate(true)->allowField(true)->save($data);
        if ($result === false) {
            $this->error($TemplateModel->getError());
        }

        $this->success("添加成功！", url("template/index"));
    }

    public function edit()
    {
        $id        = $this->request->param('id', 0, 'intval');
		$TemplateModel = new TemplateModel();
        $Template = $TemplateModel->get($id);
        $this->assign('targets', $this->targets);
        $this->assign('template', $Template);
        return $this->fetch();
    }


    public function editPost()
    {
        $data      = $this->request->param();
        $TemplateModel = new TemplateModel();
		
        $result    = $TemplateModel->validate(true)->allowField(true)->isUpdate(true)->save($data);
        if ($result === false) {
            $this->error($TemplateModel->getError());
        }

        $this->success("保存成功！", url("template/index"));
    }


    public function delete()
    {
        $id = $this->request->param('id', 0, 'intval');
        LinkModel::destroy($id);

        $this->success("删除成功！", url("template/index"));
    }

    public function listOrder()
    {
        $TemplateModel = new  TemplateModel();
        parent::listOrders($TemplateModel);
        $this->success("排序更新成功！");
    }

    public function toggle()
    {
        $data      = $this->request->param();
        $TemplateModel = new TemplateModel();

        if (isset($data['ids']) && !empty($data["display"])) {
            $ids = $this->request->param('ids/a');
            $TemplateModel->where(['id' => ['in', $ids]])->update(['status' => 1]);
            $this->success("更新成功！");
        }

        if (isset($data['ids']) && !empty($data["hide"])) {
            $ids = $this->request->param('ids/a');
            $TemplateModel->where(['id' => ['in', $ids]])->update(['status' => 0]);
            $this->success("更新成功！");
        }


    }
	public function updatestatus()
    {
        $data      = $this->request->param();
        $TemplateModel = new TemplateModel();

        if (isset($data['ids']) && !empty($data["display"])) {
            $ids = $this->request->param('ids/a');
            $TemplateModel->where(['id' => ['in', $ids]])->update(['status' => 1]);
            $this->redirect('Template/index');
        }

        if (isset($data['ids']) && !empty($data["hide"])) {
            $ids = $this->request->param('ids/a');
            $TemplateModel->where(['id' => ['in', $ids]])->update(['status' => 0]);
            $this->redirect('Template/index');
        }


    }
	//显示课题步骤
	public function progresslist(){
		$data      = $this->request->param();		
		$list=db('classprogress')->where(['mid'=>$data['id']])->select();
		$this->assign('mid', $data['id']);
		$this->assign('progressid', $data['progressid']);		
		$this->assign('list', $list);
		return $this->fetch();
	}
	//添加课题步骤
	public function addclass_progress(){
		$data      = $this->request->param();
		$this->assign('data', $data);
		return $this->fetch();
	}
	//提交课题步骤
	public function addclass_progresspost(){
		$data      = $this->request->param();
		$result=db('classprogress')->insert($data);
		if ($result === false) {
            $this->error($TemplateModel->getError());
        }
        $this->success("添加成功！", url("template/index"));
	}
	public function delclass_progress(){
		$id = $this->request->param('id', 0, 'intval');
        db('classprogress')->delete($id);
        $this->success("删除成功！");		
	}
	//显示课题步骤
	public function look(){
		$data      = $this->request->param();		
		$list=db('classprogress')->where(['mid'=>$data['id']])->select();
        for ($i=1; $i <= $data['progressid'] ; $i++) { 
            $name[$i] = TemplateAttrModel::where('template_id',$data['id'])->where('progress',$i)->value('name');
        }
		$this->assign('mid', $data['id']);
		$this->assign('progressid', $data['progressid']);		
		$this->assign('list', $list);
        $this->assign('name', $name);
		return $this->fetch();
	}

	
	
	
}
